package com.ruoyi.news.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.news.domain.TakeMoney;
import com.ruoyi.news.domain.XcxOrderMain;
import com.ruoyi.news.domain.XcxWalletInfo;
import com.ruoyi.news.domain.XxcWallet;
import com.ruoyi.news.mapper.XcxWalletInfoMapper;
import com.ruoyi.news.service.ITakeMoneyService;
import com.ruoyi.news.service.IXcxWalletInfoService;
import com.ruoyi.news.service.IXxcWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 小程序-钱包明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class XcxWalletInfoServiceImpl implements IXcxWalletInfoService
{
    @Autowired
    private XcxWalletInfoMapper xcxWalletInfoMapper;

    @Autowired
    private IXxcWalletService xxcWalletService;

    @Autowired
    private ITakeMoneyService takeMoneyService;

    /**
     * 查询小程序-钱包明细
     * 
     * @param id 小程序-钱包明细主键
     * @return 小程序-钱包明细
     */
    @Override
    public XcxWalletInfo selectXcxWalletInfoById(String id)
    {
        return xcxWalletInfoMapper.selectXcxWalletInfoById(id);
    }

    /**
     * 查询小程序-钱包明细列表
     * 
     * @param xcxWalletInfo 小程序-钱包明细
     * @return 小程序-钱包明细
     */
    @Override
    public List<XcxWalletInfo> selectXcxWalletInfoList(XcxWalletInfo xcxWalletInfo)
    {
        return xcxWalletInfoMapper.selectXcxWalletInfoList(xcxWalletInfo);
    }

    /**
     * 新增小程序-钱包明细
     * 
     * @param xcxWalletInfo 小程序-钱包明细
     * @return 结果
     */
    /*
     *  1 + 充值
     *  2 - 付款
     *  3 + 取消订单
     *  4 + 完成订单
     *  5 - 修改金额
     * */
    @Override
    public int insertXcxWalletInfo(XcxWalletInfo xcxWalletInfo, SysUser sysUser)
    {
        xcxWalletInfo.setCreateTime(DateUtils.getNowDate());

        if(xcxWalletInfo.getType().equals("1")){ // 充值
            xcxWalletInfo.setCreateBy(sysUser.getUserId()+"");
            //修改余额
            XxcWallet xcxWallet = xxcWalletService.selectXxcWalletByUserId(xcxWalletInfo.getUserId());
            BigDecimal b = new BigDecimal(xcxWalletInfo.getMoney());
            BigDecimal sum = xcxWallet.getYuE().add(b);
            xcxWallet.setYuE(sum);
            xxcWalletService.updateXxcWallet(xcxWallet);
        }else if(xcxWalletInfo.getType().equals("2")){ // 付款
            XcxOrderMain xcxOrderMain = xcxWalletInfo.getOrderMain();
            BigDecimal nowPrice = new BigDecimal(xcxOrderMain.getPrice());

            //修改钱包
            XxcWallet xxcWallet = xxcWalletService.selectXxcWalletByUserId(sysUser.getUserId()+"");
            xxcWallet.setYuE( xxcWallet.getYuE().subtract(nowPrice) );
            xxcWallet.setSuoE( xxcWallet.getSuoE().add(nowPrice) );
            xxcWalletService.updateXxcWallet(xxcWallet);

            //明细金额
            xcxWalletInfo.setMoney(nowPrice+"");

        }else if(xcxWalletInfo.getType().equals("3")){ // 接方-取消订单
            //修改钱包
            XcxOrderMain temp = xcxWalletInfo.getOrderMain();
            BigDecimal raid = new BigDecimal(xcxWalletInfo.getRaid());
            BigDecimal price = new BigDecimal(temp.getPrice());
            BigDecimal yj = price.multiply(raid);
            //平台收款 strat
            TakeMoney takeMoney = new TakeMoney();
            takeMoney.setMoney( yj+"" );
            takeMoney.setOldMoney(temp.getPrice());
            takeMoney.setRaid(raid+"");
            takeMoney.setSource("1");
            takeMoney.setSourceId(temp.getOrderId());
            takeMoney.setPayUserId(temp.getCreateBy());
            takeMoney.setId(IdUtils.fastSimpleUUID());
            int takePt = takeMoneyService.insertTakeMoney(takeMoney);
            //平台收款 end
            if(takePt > 0){
                XxcWallet xxcWallet = xxcWalletService.selectXxcWalletByUserId(temp.getCreateBy());
                // 此次用户得到
                // 余额 +（订单金额-佣金）;
                xxcWallet.setYuE( xxcWallet.getYuE().add(price.subtract(yj)) );
                // 锁额 -（订单金额）
                xxcWallet.setSuoE( xxcWallet.getSuoE().subtract(price) );
                xxcWalletService.updateXxcWallet(xxcWallet);

                //设置明细
                xcxWalletInfo.setCreateBy(xcxWalletInfo.getOrderMain().getCreateBy());
                xcxWalletInfo.setMoney(price.subtract(yj)+"");
            }
        }else if (xcxWalletInfo.getType().equals("4")){ //确认付款
            //修改钱包
            XcxOrderMain temp = xcxWalletInfo.getOrderMain();
            BigDecimal raid = new BigDecimal(xcxWalletInfo.getRaid());
            BigDecimal price = new BigDecimal(temp.getPrice());
            BigDecimal yj = price.multiply(raid);
            //平台收款
            TakeMoney takeMoney = new TakeMoney();
            takeMoney.setMoney( yj+"" );
            takeMoney.setOldMoney(temp.getPrice());
            takeMoney.setRaid(raid+"");
            takeMoney.setSource("1");
            takeMoney.setSourceId(temp.getOrderId());
            takeMoney.setPayUserId(temp.getCreateBy());
            takeMoney.setId(IdUtils.fastSimpleUUID());
            int takePt = takeMoneyService.insertTakeMoney(takeMoney);
            if(takePt > 0){
                XxcWallet xxcWallet = xxcWalletService.selectXxcWalletByUserId(temp.getTakeUser());
                // 此次用户得到
                // 余额 +（订单金额-佣金）;
                xxcWallet.setYuE( xxcWallet.getYuE().add(price.subtract(yj)) );
                xxcWalletService.updateXxcWallet(xxcWallet);

                //付方减去锁定金额
                XxcWallet xxcWalletSend = xxcWalletService.selectXxcWalletByUserId(temp.getCreateBy());
                xxcWalletSend.setSuoE( xxcWalletSend.getSuoE().subtract(price) );
                xxcWalletService.updateXxcWallet(xxcWalletSend);

                //设置明细
                xcxWalletInfo.setCreateBy(xcxWalletInfo.getOrderMain().getCreateBy());
                xcxWalletInfo.setMoney(price.subtract(yj)+"");
            }
        }else if(xcxWalletInfo.getType().equals("5")){ //修改订单金额
            XcxOrderMain xcxOrderMain = xcxWalletInfo.getOrderMain();
            BigDecimal nowPrice = new BigDecimal(xcxOrderMain.getPrice());

            //修改钱包
            XxcWallet xxcWallet = xxcWalletService.selectXxcWalletByUserId(xcxOrderMain.getCreateBy()+"");
            BigDecimal ye =   xxcWallet.getYuE().add(xcxWalletInfo.getOldPrice()).subtract(xcxWalletInfo.getNewPrice());
            BigDecimal se =   xxcWallet.getSuoE().subtract(xcxWalletInfo.getOldPrice()).add(xcxWalletInfo.getNewPrice());
            xxcWallet.setYuE( ye );
            xxcWallet.setSuoE( se );
            xxcWalletService.updateXxcWallet(xxcWallet);

            //明细金额
            xcxWalletInfo.setMoney(xcxWalletInfo.getNewPrice().subtract(xcxWalletInfo.getOldPrice())+"");
        }else {
            //修改余额
            XxcWallet xcxWallet = xxcWalletService.selectXxcWalletByUserId(xcxWalletInfo.getUserId());
            BigDecimal b = new BigDecimal(xcxWalletInfo.getMoney());
            BigDecimal sum = xcxWallet.getYuE().subtract(b);
            xcxWallet.setYuE(sum);
            xxcWalletService.updateXxcWallet(xcxWallet);
        }
        return xcxWalletInfoMapper.insertXcxWalletInfo(xcxWalletInfo);
    }

    @Override
    public int insertGoodsXcxWalletInfo(XcxWalletInfo xcxWalletInfo, SysUser sysUser) {
        xcxWalletInfo.setCreateTime(DateUtils.getNowDate());

        if(xcxWalletInfo.getType().equals("6")){ // 购买商品扣款
            xcxWalletInfo.setCreateBy(sysUser.getUserId()+"");
            //修改余额
            XxcWallet xcxWallet = xxcWalletService.selectXxcWalletByUserId(xcxWalletInfo.getUserId());
            BigDecimal b = xcxWalletInfo.getGoodsOrder().getPrice();
            BigDecimal sum = xcxWallet.getYuE().subtract(b);
            xcxWallet.setYuE(sum);
            //平台收款
            TakeMoney takeMoney = new TakeMoney();
            takeMoney.setMoney( xcxWalletInfo.getGoodsOrder().getPrice()+"" );
            takeMoney.setOldMoney(xcxWalletInfo.getGoodsOrder().getPrice()+"");
            takeMoney.setRaid("0.00");
            takeMoney.setSource("2");
            takeMoney.setSourceId(xcxWalletInfo.getGoodsOrder().getOrderId());
            takeMoney.setPayUserId(xcxWalletInfo.getGoodsOrder().getCreateBy());
            takeMoney.setId(IdUtils.fastSimpleUUID());
            int takePt = takeMoneyService.insertTakeMoney(takeMoney);
            xxcWalletService.updateXxcWallet(xcxWallet);
        }else if(xcxWalletInfo.getType().equals("7")){
            //修改余额
            XxcWallet xcxWallet = xxcWalletService.selectXxcWalletByUserId(xcxWalletInfo.getUserId());
            BigDecimal b = xcxWalletInfo.getGoodsOrder().getPrice();
            BigDecimal sum = xcxWallet.getYuE().add(b);
            xcxWallet.setYuE(sum);
            //平台收款

            TakeMoney takeMoney = takeMoneyService.selectTakeMoneyBySourceId(xcxWalletInfo.getGoodsOrder().getOrderId());
            takeMoney.setDelFlag("1");
            takeMoneyService.updateTakeMoney(takeMoney);
            xxcWalletService.updateXxcWallet(xcxWallet);
        }
        else if(xcxWalletInfo.getType().equals("8")){
            //修改余额
            XxcWallet xcxWallet = xxcWalletService.selectXxcWalletByUserId(xcxWalletInfo.getUserId());
            BigDecimal b = xcxWalletInfo.getGoodsOrder().getPrice();
            BigDecimal sum = xcxWallet.getYuE().add(b);
            xcxWallet.setYuE(sum);
            //平台收款

            TakeMoney takeMoney = takeMoneyService.selectTakeMoneyBySourceId(xcxWalletInfo.getGoodsOrder().getOrderId());
            takeMoney.setDelFlag("1");
            takeMoneyService.updateTakeMoney(takeMoney);
            xxcWalletService.updateXxcWallet(xcxWallet);
        }
        return xcxWalletInfoMapper.insertXcxWalletInfo(xcxWalletInfo);
    }

    /**
     * 修改小程序-钱包明细
     * 
     * @param xcxWalletInfo 小程序-钱包明细
     * @return 结果
     */
    @Override
    public int updateXcxWalletInfo(XcxWalletInfo xcxWalletInfo)
    {
        xcxWalletInfo.setUpdateTime(DateUtils.getNowDate());
        return xcxWalletInfoMapper.updateXcxWalletInfo(xcxWalletInfo);
    }

    /**
     * 批量删除小程序-钱包明细
     * 
     * @param ids 需要删除的小程序-钱包明细主键
     * @return 结果
     */
    @Override
    public int deleteXcxWalletInfoByIds(String[] ids)
    {
        return xcxWalletInfoMapper.deleteXcxWalletInfoByIds(ids);
    }

    /**
     * 删除小程序-钱包明细信息
     * 
     * @param id 小程序-钱包明细主键
     * @return 结果
     */
    @Override
    public int deleteXcxWalletInfoById(String id)
    {
        return xcxWalletInfoMapper.deleteXcxWalletInfoById(id);
    }
}
